/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Responses;

import Beans.Responses.DataRetrievalComponents.DataRetrievalComponent;
import Utilities.Constants.ResourceRequestType;
import Utilities.ApplicationException;
import java.util.ArrayList;

/**
 * Instances of this class are used when returning a response to a data retrieval
 * request.
 *
 * @param <T> the type of data component that is within the response
 *
 * @author Angel Sanadinov
 */
public class DataRetrievalResponse<T extends DataRetrievalComponent> extends ResponseBean
{
    private T resource;                             //single response resource
    private ArrayList<T> resourcesList;             //multiple response resources
    private ResourceRequestType resourceRequestType;//type of requested resources

    /**
     * Constructs the response object using the supplied response message.
     *
     * @param exception an exception that occurred during request processing
     */
    public DataRetrievalResponse(ApplicationException exception)
    {
        super(false, exception);
        this.resource = null;
        this.resourcesList = null;
        this.resourceRequestType = ResourceRequestType.INVALID_RESOURCE;
    }

    /**
     * Constructs the response object using a single response resource.
     *
     * @param resource the response resource
     */
    public DataRetrievalResponse(T resource)
    {
        super(true, null);
        this.resource = resource;
        this.resourcesList = null;
        this.resourceRequestType = ResourceRequestType.SINGLE_RESOURCE;
    }

    /**
     * Constructs the response object using multiple resources.
     *
     * @param resourcesList the list of response resources
     */
    public DataRetrievalResponse(ArrayList<T> resourcesList)
    {
        super(true, null);
        this.resource = null;
        this.resourcesList = resourcesList;
        this.resourceRequestType = ResourceRequestType.MULTIPLE_RESOURCES;
    }

    /**
     * Retrieves the resource (for single resource responses).
     *
     * @return the resource, if available or <code>null</code> otherwise
     */
    public T getResource()
    {
        if(resourceRequestType == ResourceRequestType.SINGLE_RESOURCE)
            return resource;
        else
            return null;
    }

    /**
     * Retrieves all response resources (for multiple resource responses).
     *
     * @return the list of resources, if available or <code>null</code> otherwise
     */
    public ArrayList<T> getResourcesList()
    {
        if(resourceRequestType == ResourceRequestType.MULTIPLE_RESOURCES)
            return resourcesList;
        else
            return null;
    }

    /**
     * Retrieves the type of requested resources.
     *
     * @return the resource request type
     */
    public ResourceRequestType getResourceRequestType()
    {
        return resourceRequestType;
    }

    @Override
    public ArrayList<ApplicationException> getExceptions()
    {
        return null;
    }

    /**
     * Checks the validity of the response. <br><br>
     *
     * A response is considered valid only if all of its data is valid.
     *
     * @return <code>true</code> if the response is valid or <code>false</code> otherwise
     */
    @Override
    public boolean isValid()
    {
        boolean result = false;

        switch(resourceRequestType)
        {
            case SINGLE_RESOURCE:
            {
                if(resource != null && resource.isValid())
                    result = true;
                else
                    ;
            } break;

            case MULTIPLE_RESOURCES:
            {
                if(resourcesList != null)
                {
                    for(DataRetrievalComponent component : resourcesList)
                    {
                        if(!component.isValid())
                            return false;
                        else
                            ;
                    }

                    result = true;
                }
                else
                    ;
            } break;

            default: result = true;
        }

        return result;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (super.toString)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return DataRetrievalResponse.class.getSimpleName() + ": (" + super.toString() + ")";
    }
}
